﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace ClickToCall.CustomControls
{
    public partial class Form : System.Windows.Forms.Form
    {
        Bitmap titleBar, titleBarCurve;
        Point startPoint;
        bool isDrag = false;
        Font activeFont;
        Color activeColor;

        public Form()
        {
            InitializeComponent();
            titleBar = new Bitmap(
                    Properties.Resources.titlebargradient,
                    new Size(
                        Properties.Resources.titlebargradient.Width,
                        Properties.Resources.titlebargradient.Height
                        )
                    );
            titleBarCurve = new Bitmap(
                    Properties.Resources.titlebarcurve,
                    new Size(
                        Properties.Resources.titlebarcurve.Width,
                        Properties.Resources.titlebarcurve.Height
                        )
                    );
            this.FormBorderStyle = FormBorderStyle.None;
            this.Padding = new Padding(4, titleBar.Height+4, 4, 4);
            paintForm();

            activeFont = new Font(this.Font, FontStyle.Bold);
            activeColor = Color.WhiteSmoke;
        }

        private ContentAlignment _textAlign = ContentAlignment.MiddleCenter;
        public ContentAlignment TextAlign
        {
            get { return this._textAlign; }
            set
            {
                this._textAlign = value;
                this.Invalidate();
            }
        }

        private void paintForm()
        {
            LinearGradientBrush innerBrush = new LinearGradientBrush(
                new RectangleF(0, 0, this.Width, this.Height),
                Color.FromArgb(52, 62, 70),
                Color.FromArgb(83, 102, 105), 
                LinearGradientMode.Vertical);
            
            LinearGradientBrush edgeBrush = new LinearGradientBrush(
                new RectangleF(0, 0, 4, this.Height),
                Color.FromArgb(103, 110, 116),
                Color.FromArgb(125, 140, 143),
                LinearGradientMode.Vertical);

            Bitmap bmp = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(innerBrush, new Rectangle(0, 0, this.Width, this.Height));
            g.DrawLine(new Pen(edgeBrush, 4), 0, 0, 0, this.Height);
            g.DrawLine(new Pen(edgeBrush, 2), this.Width, 0, this.Width, this.Height);
            g.DrawLine(new Pen(Color.FromArgb(103, 110, 116),4), 0, 0, this.Width, 0);
            g.DrawLine(new Pen(Color.FromArgb(125, 140, 143),4), 0, this.Height, this.Width, this.Height);

            this.BackgroundImage = bmp;
            this.BackgroundImageLayout = ImageLayout.Stretch;

            innerBrush.Dispose();
            innerBrush = null;
            edgeBrush.Dispose();
            edgeBrush = null;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.isDrag)
            {
                Point p = Control.MousePosition;
                p.X = p.X - this.startPoint.X;
                p.Y = p.Y - this.startPoint.Y;
                this.Location = p;
            }

            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.startPoint = new Point(e.X, e.Y);
            if (e.Button == MouseButtons.Left &&
                e.Y < this.titleBar.Height)
            {
                this.isDrag = true;
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.isDrag = false;
            base.OnMouseUp(e);
        }

        protected override void OnActivated(EventArgs e)
        {
            this.activeColor = Color.WhiteSmoke;
            this.Invalidate();
            base.OnActivated(e);
        }

        protected override void OnDeactivate(EventArgs e)
        {
            this.activeColor = Color.Gray;
            this.Invalidate();
            base.OnDeactivate(e);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
        {
            Graphics g = pe.Graphics;
            int curvePad = 22;
            int top = 2;
            TextureBrush titleBarBrush = new TextureBrush(this.titleBar, System.Drawing.Drawing2D.WrapMode.Tile);
            titleBarBrush.TranslateTransform(0, 2);
            Rectangle startRect = new Rectangle(2, top, curvePad, 21);
            Rectangle endRect = new Rectangle(
                curvePad + titleBarCurve.Width,
                top,
                (this.Width - 2) - (this.titleBarCurve.Width + curvePad),
                21);
            g.FillRectangle(titleBarBrush, startRect);
            g.DrawImage(titleBarCurve, new Rectangle(curvePad, top, titleBarCurve.Width, 21));
            g.FillRectangle(titleBarBrush, endRect);

            Bitmap png = new Bitmap(Properties.Resources.ctoc_pc, new Size(16, 16));
            g.DrawImage(png, 4, 5, png.Width, png.Height);


            if (this.Text.Length > 0)
            {
                SizeF sz = g.MeasureString(this.Text, this.Font);
                
                g.DrawString(this.Text,
                              activeFont,
                              new SolidBrush(activeColor),
                              50,7
                             );
            }

            base.OnPaint(pe);
        }

    }
}
